package com.event.controller;

import com.event.pojo.entity.Article;
import com.event.result.PageResult;
import com.event.result.Result;
import com.event.service.ArticleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
	
	@Resource
	private ArticleService articleService;
	
	/**
	 * 新增文章
	 */
	@PostMapping
	public Result<?> addArticle(@RequestBody Article article) {
		articleService.addArticle(article);
		return Result.success();
	}
	
	/**
	 * 分页查询文章
	 */
	@GetMapping
	public Result<PageResult<Article>> getArticleList(@RequestParam(required = false, defaultValue = "1") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer pageSize, @RequestParam(required = false, defaultValue = "") Integer categoryId, String state) {
		log.info("pageNum:{}, pageSize:{}, categoryId:{}, state:{}", pageNum, pageSize, categoryId, state);
		return Result.success(articleService.getArticleList(pageNum, pageSize, categoryId, state));
	}
	
	/**
	 * 获取文章详情
	 */
	@GetMapping("/detail")
	public Result<Article> getArticle(Integer id) {
		log.info("根据id获取文章:{}", id);
		return Result.success(articleService.getById(id));
	}
	
	/**
	 * 更新文章
	 */
	@PutMapping
	public Result<?> updateArticle(@RequestBody Article article) {
		log.info("更新文章:{}", article);
		articleService.updateArticle(article);
		return Result.success();
	}
	
	/**
	 * 删除文章
	 */
	@DeleteMapping
	public Result<?> deleteArticle(Integer id) {
		log.info("根据id删除文章:{}", id);
		articleService.removeById(id);
		return Result.success();
	}
}
